﻿//  @ Project : SPPager
//  @ File Name : IPageList.cs
//  @ Date : 2013/6/5
//  @ Author : yubaolee(.cpp)
//  @ Description: Use the default stored procedure ".LYBPager" 

using System;
using System.Data.SqlClient;
using System.Data;

namespace LYB.NET.SPPager
{
    ///<summary>
    ///<para>the default stored procedure ".LYBPager"</para> 
    ///<para>*TableName --database table name </para> 
    ///<para>*PrimaryKey --table primary key  </para>
    ///<para>*SortField --table sort field，such as：id desc (id desc,dt asc)</para>
    ///<para>QueryField --table query field('*' means all fields)  </para>
    ///<para>Where --sql where (string without word 'where')  </para>
    ///<para>OrderBy --(0==asc, 1==desc)  </para>
    ///<para>PageCount --total page count[readonly] </para> 
    /// </summary>
    public class LYBSPPager : StoredProcedureBase
    {
        public string TableName { get; set; }
        public string QueryField { get; set; }
        public string PrimaryKey { get; set; }
        public string SortField { get; set; }
        public string Where { get; set; }
        public int OrderBy { get; set; }

        public LYBSPPager(string strconn)
            : base(strconn, ".LYBPager") { }

        /// <param name="strconn">sql connection string</param>
        /// <param name="tablename">database table name </param>
        /// <param name="primarykey">table primary key</param>
        /// <param name="sortfield">table sort field，such as：id desc (id desc,dt asc)</param>
        public LYBSPPager(string strconn, string tablename, string primarykey, string sortfield)
            : base(strconn, ".LYBPager")
        {
            TableName = tablename;
            PrimaryKey = primarykey;
            SortField = sortfield;
        }

        public override SqlParameter[] Parament
        {
            get
            {
                if (string.IsNullOrEmpty(TableName))
                    throw new ApplicationException("LYBSPPager对象TableName属性不能为空");

                if (string.IsNullOrEmpty(PrimaryKey))
                    throw new ApplicationException("LYBSPPager对象PrimaryKey属性不能为空");

                if (string.IsNullOrEmpty(SortField))
                    throw new ApplicationException("LYBSPPager对象SortField属性不能为空");

                SqlParameter[] para = new SqlParameter[]{  
                new SqlParameter("@strTable",SqlDbType.VarChar,-1),  
                new SqlParameter("@strField",SqlDbType.VarChar,-1),  
                new SqlParameter("@pageSize",SqlDbType.Int),  
                new SqlParameter("@pageIndex",SqlDbType.Int),  
                new SqlParameter("@strSortKey",SqlDbType.VarChar,-1),  
                new SqlParameter("@strSortField",SqlDbType.VarChar,-1), 
                new SqlParameter("@strOrderBy",SqlDbType.Bit),  
                new SqlParameter("@return",SqlDbType.Int),  
                new SqlParameter("@strWhere",SqlDbType.VarChar,-1)
                };

                para[0].Value = TableName;
                para[1].Value = QueryField;
                para[2].Value = PageSize;
                para[3].Value = CurrentPageIndex;
                para[4].Value = PrimaryKey;
                para[5].Value = SortField;
                para[6].Value = OrderBy;
                para[7].Direction = ParameterDirection.ReturnValue;
                para[8].Value = Where;

                return para; 
            }
        }

        public override int TotalRecordCount
        {
            get
            {
                return int.Parse(Command.Parameters["@return"].Value.ToString());
            }
        }
    }
}
